package com.ruoyi.base.service.impl;

import java.util.*;

import com.ruoyi.base.mapper.BaseEmerTeTempMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BaseEmerTeListMapper;
import com.ruoyi.base.domain.BaseEmerTeList;
import com.ruoyi.base.service.IBaseEmerTeListService;

/**
 * 应急TE单清单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-23
 */
@Service
public class BaseEmerTeListServiceImpl implements IBaseEmerTeListService 
{
    @Autowired
    private BaseEmerTeListMapper baseEmerTeListMapper;

    @Autowired
    private BaseEmerTeTempMapper baseEmerTeTempMapper;
    /**
     * 查询应急TE单清单
     * 
     * @param ID 应急TE单清单主键
     * @return 应急TE单清单
     */
    @Override
    public BaseEmerTeList selectBaseEmerTeListByID(String ID)
    {
        return baseEmerTeListMapper.selectBaseEmerTeListByID(ID);
    }
    /**
     * 查询应急TE单清单
     *
     * @param view 场景
     * @param position 岗位
     * @return 应急TE单清单
     */
    @Override
    public BaseEmerTeList selectBaseEmerTeListByViewAndPosition(String view, String position,  String unitCode)
    {
        return baseEmerTeListMapper.selectBaseEmerTeListByViewAndPosition(view, position, unitCode);
    }

    /**
     * 查询应急TE单清单列表
     * 
     * @param baseEmerTeList 应急TE单清单
     * @return 应急TE单清单
     */
    @Override
    public List<BaseEmerTeList> selectBaseEmerTeListList(BaseEmerTeList baseEmerTeList)
    {
        return baseEmerTeListMapper.selectBaseEmerTeListList(baseEmerTeList);
    }

    /**
     * 新增应急TE单清单
     * 
     * @param baseEmerTeList 应急TE单清单
     * @return 结果
     */
    @Override
    public int insertBaseEmerTeList(BaseEmerTeList baseEmerTeList)
    {
        int result = baseEmerTeListMapper.insertBaseEmerTeList(baseEmerTeList);
        //新增检查单明细模板
        if(Objects.nonNull(baseEmerTeList.getBaseEmerTeTemps())){
            baseEmerTeList.getBaseEmerTeTemps().forEach(baseEmerTeTemp -> {
                baseEmerTeTemp.setID(UUID.randomUUID().toString().replace("-",""));
                baseEmerTeTemp.setCreatedBy(baseEmerTeList.getCreatedBy());
                baseEmerTeTemp.setCreatedTime(new Date());
                baseEmerTeTemp.setEmerTeId(baseEmerTeList.getID());
                baseEmerTeTempMapper.insertBaseEmerTeTemp(baseEmerTeTemp);
            });
        }
        return result;
    }

    /**
     * 修改应急TE单清单
     * 
     * @param baseEmerTeList 应急TE单清单
     * @return 结果
     */
    @Override
    public int updateBaseEmerTeList(BaseEmerTeList baseEmerTeList)
    {
        int result = baseEmerTeListMapper.updateBaseEmerTeList(baseEmerTeList);
        List<String>tempIds = new ArrayList<>();
        //更新检查的明细模板
        if(Objects.nonNull(baseEmerTeList.getBaseEmerTeTemps())){
            baseEmerTeList.getBaseEmerTeTemps().forEach(baseEmerTeTemp -> {
                if(Objects.isNull(baseEmerTeTemp.getID())){
                    String ID = UUID.randomUUID().toString().replace("-","");
                    tempIds.add(ID);
                    baseEmerTeTemp.setID(ID);
                    baseEmerTeTemp.setCreatedBy(baseEmerTeList.getCreatedBy());
                    baseEmerTeTemp.setCreatedTime(baseEmerTeList.getCreatedTime());
                    baseEmerTeTemp.setEmerTeId(baseEmerTeList.getID());
                    baseEmerTeTempMapper.insertBaseEmerTeTemp(baseEmerTeTemp);
                }else {
                    tempIds.add(baseEmerTeTemp.getID());
                    baseEmerTeTemp.setUpdatedBy(baseEmerTeList.getUpdatedBy());
                    baseEmerTeTemp.setUpdatedTime(new Date());
                    baseEmerTeTempMapper.updateBaseEmerTeTemp(baseEmerTeTemp);
                }
           });
       }
        //删除检查单明细模板----这一块逻辑有问题  需要调整 将页面上已经删除的数据删除
        BaseEmerTeList dbBaseEmerTeList = baseEmerTeListMapper.selectBaseEmerTeListByID(baseEmerTeList.getID());
        if (Objects.nonNull(dbBaseEmerTeList.getBaseEmerTeTemps())) {
            dbBaseEmerTeList.getBaseEmerTeTemps().forEach(baseEmerTeTemp -> {
                if (!tempIds.contains(baseEmerTeTemp.getID())) {
                    baseEmerTeTempMapper.deleteBaseEmerTeTempByID(baseEmerTeTemp.getID());
                }
            });
        }
        return result;
    }

    /**
     * 批量删除应急TE单清单
     * 
     * @param IDs 需要删除的应急TE单清单主键
     * @return 结果
     */
    @Override
    public int deleteBaseEmerTeListByIDs(String[] IDs)
    {
        //删除检查单明细模板
        if (Objects.nonNull(IDs) && IDs.length > 0){
            Arrays.stream(IDs).forEach(ID -> {
                baseEmerTeTempMapper.deleteBaseEmerTeTempByTeID(ID);
            });
        }
        return baseEmerTeListMapper.deleteBaseEmerTeListByIDs(IDs);
    }

    /**
     * 删除应急TE单清单信息
     * 
     * @param ID 应急TE单清单主键
     * @return 结果
     */
    @Override
    public int deleteBaseEmerTeListByID(String ID)
    {
        //删除检查单明细模板
        baseEmerTeTempMapper.deleteBaseEmerTeTempByTeID(ID);
        return baseEmerTeListMapper.deleteBaseEmerTeListByID(ID);
    }
}
